package com.zeyi.chat.controller;

import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.StageManager;
import com.zeyi.chat.manager.UserManager;
import com.zeyi.chat.utils.SocketUtil;
import com.zeyi.chat.views.Alert;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author zeyi
 */
public class AddFriendController{
    @FXML
    private Label addId;

    @FXML
    private Label warningText;

    @FXML
    private Label addType;

    @FXML
    private VBox queryContainer;

    @FXML
    private TextField queryText;

    @FXML
    private Label friendLabel;

    @FXML
    private Label groupLabel;

    private String findType = "0";

    @FXML
    public void minimize(MouseEvent mouseEvent) {
        Stage stage = (Stage) friendLabel.getScene().getWindow();
        stage.setIconified(true);
    }

    @FXML
    public void onClose(MouseEvent mouseEvent) {
        Stage stage = (Stage) friendLabel.getScene().getWindow();
        stage.close();
        StageManager.setFriendStage(null);
    }

    @FXML
    public void findFriend(MouseEvent mouseEvent) {
        friendLabel.setStyle("-fx-text-fill: #0188FB");
        groupLabel.setStyle("-fx-text-fill: #909399");
        findType = "0";
        queryText.clear();
        friendLabel.requestFocus();
        queryText.setPromptText("请输入好友邮箱");
    }

    @FXML
    public void findGroup(MouseEvent mouseEvent) {
        groupLabel.setStyle("-fx-text-fill: #0188FB");
        friendLabel.setStyle("-fx-text-fill: #909399");
        findType = "1";
        queryText.clear();
        groupLabel.requestFocus();
        queryText.setPromptText("请输入群号");
    }

    /**
     * 查询
     */
    @FXML
    public void query(ActionEvent actionEvent) {
        String text = queryText.getText();
        if (text.equals("")) {
            if (findType.equals("0")) {
                warningText.setText("请输入好友号");
            } else {
                warningText.setText("请输入群号");
            }
        } else {
            warningText.setText("");
            //清除上次搜索结果
            int size = queryContainer.getChildren().size();
            if (size == 2) queryContainer.getChildren().remove(1);
            Message message = null;
            if (findType.equals("0")) {
                message = new Message("QueryHandler/queryFriend", "QueryHandler/queryFriend", text);
            } else {
                message = new Message("QueryHandler/queryGroup", "QueryHandler/queryGroup", text);
            }
            SocketUtil.send(message);
        }
    }

    /**
     * 添加
     */
    public void addFriend(ActionEvent actionEvent) {
        Message message = null;
        if (Integer.valueOf(addId.getText()).equals(UserManager.getCurrentUser().getId())) {
            new Alert("不能添加自己", (Stage) addId.getScene().getWindow());
            return;
        }
        if (addType.getText().equals("0")) {
            message = new Message("AddHandler/addFriend", "AddHandler/addFriend", UserManager.getCurrentUser(), addId.getText());
        } else {
            message = new Message("AddHandler/joinGroup", "AddHandler/joinGroup",  UserManager.getCurrentUser(), addId.getText());
        }
        SocketUtil.send(message);
    }
}
